//
//  XLTabBarButton.m
//  XLTabBarController
//
//  Created by ZxlAndZyp on 2019/1/8.
//  Copyright © 2019 zuoxueliang. All rights reserved.
//

#import "XLTabBarButton.h"
#import "XLUtils/UIView+XLExtension.h"

@implementation XLTabBarButton

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.imageView];
        [self addSubview:self.titleL];
        self.backgroundColor = [UIColor grayColor];

    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGSize imageSize = [[XLTabBarConfig tabBarConfig] imageSize];
    CGFloat imageX = self.zxl_width * 0.5 - imageSize.width * 0.5;
    CGFloat imageY = [[XLTabBarConfig tabBarConfig] imageOffset];
    if ([[XLTabBarConfig tabBarConfig] layoutType] == XLLayoutTypeImage) {
        imageY = self.zxl_height * 0.5 - imageSize.height * 0.5;
    }
    self.imageView.frame = CGRectMake(imageX, imageY, imageSize.width, imageSize.height);
    
    CGFloat titleX = 4.0f;
    CGFloat titleH = 14.0f;
    CGFloat titleW = self.zxl_width-8;
    CGFloat titleY = self.zxl_height - titleH - [[XLTabBarConfig tabBarConfig] titleOffset];
    self.titleL.frame = CGRectMake(titleX, titleY, titleW, titleH);
}

- (void)setLayoutType:(XLLayoutType)layoutType
{
    _layoutType = layoutType;
    if (layoutType == XLLayoutTypeImage) {
        self.titleL.hidden = YES;
    }
}

#pragma mark-
#pragma mark set&&get
- (UIImageView *)imageView
{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}

- (UILabel *)titleL
{
    if (!_titleL) {
        _titleL = [[UILabel alloc] init];
        _titleL.textAlignment = NSTextAlignmentCenter;
        _titleL.font = [UIFont systemFontOfSize:[[XLTabBarConfig tabBarConfig] titleFont]];
    }
    return _titleL;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
